<?
session_start();

include('./../config/db_settings.php');
include('./../pms/pms_inc.php');

$user = $_SESSION[$settings['session_prefix'].'user_id'];
if(empty($user)){
	echo "";
	exit;
}

$t = $_GET['t'];
$pid = $_GET['id'];

$t = intval($t);
if($t!=1 && $t!=-1 && $t!=2){
	echo "";
	exit;
}

if(mysql_error()==""){
	// check if the user has set thumbs up/down to this post
	$sql = mysql_query ("SELECT count(*) t FROM ".$db_settings['thumbsdetails_table']." WHERE pid = '".$pid."' and uid='".$user."'");
	$row = mysql_fetch_array ($sql);
	$count = $row['t'];
	if($count>0){
		echo "";
		exit;
	}

	// check if the user is the post owner
	$sql = mysql_query ("SELECT count(*) t FROM ".$db_settings['forum_table']." WHERE id = '".$pid."' and uid='".$user."'");
	$row = mysql_fetch_array ($sql);
	$count = $row['t'];
	if($count>0){
		echo "";
		exit;
	}

	// insert into details table
	if($t==1){
		mysql_query ("INSERT into ".$db_settings['thumbsdetails_table']." (pid, uid, up) values($pid,$user,1)");
	}
	elseif($t==-1){
		mysql_query ("INSERT into ".$db_settings['thumbsdetails_table']." (pid, uid, down) values($pid,$user,1)");
	}
	elseif($t==2){
		mysql_query ("INSERT into ".$db_settings['thumbsdetails_table']." (pid, uid, flower) values($pid,$user,1)");
	}

	$idExists = 0;
	$sql = mysql_query ("SELECT count(*) t FROM ".$db_settings['thumbssummary_table']." WHERE pid = '$pid'");
	$row = mysql_fetch_array ($sql);
	$count = $row['t'];
	if($count>0){
		$idExists = 1;
	}

	if($idExists>0){
		if($t==1){
			mysql_query ("UPDATE ".$db_settings['thumbssummary_table']." set up = up + 1 where pid = '$pid'");
		}
		elseif($t==-1){
			mysql_query ("UPDATE ".$db_settings['thumbssummary_table']." set down = down + 1 where pid = '$pid'");
		}
		elseif($t==2){
			mysql_query ("UPDATE ".$db_settings['thumbssummary_table']." set flower = flower + 1 where pid = '$pid'");
		}
	}else{
		// now lock the summary table and update it
		mysql_query("LOCK TABLES ".$db_settings['thumbssummary_table']." WRITE;");
		$idExists = 0;
		$sql = mysql_query ("SELECT count(*) t FROM ".$db_settings['thumbssummary_table']." WHERE pid = '$pid'");
		$row = mysql_fetch_array ($sql);
		$count = $row['t'];
		if($count>0){
			$idExists = 1;
		}
		//echo "count=".$count;
		if($idExists==1){
			if($t==1){
				mysql_query ("UPDATE ".$db_settings['thumbssummary_table']." set up = up + 1 where pid = '$pid'");
			}
			elseif($t==-1){
				mysql_query ("UPDATE ".$db_settings['thumbssummary_table']." set down = down + 1 where pid = '$pid'");
			}
			elseif($t==2){
				mysql_query ("UPDATE ".$db_settings['thumbssummary_table']." set flower = flower + 1 where pid = '$pid'");
			}
		}
		else{
			//add a new one
			if($t==1){
				mysql_query ("INSERT into ".$db_settings['thumbssummary_table']." (pid, up) values($pid,1)");
			}
			elseif($t==-1){
				mysql_query ("INSERT into ".$db_settings['thumbssummary_table']." (pid, down) values($pid,1)");
			}
			elseif($t==2){
				mysql_query ("INSERT into ".$db_settings['thumbssummary_table']." (pid, flower) values($pid,1)");
			}
		}

		mysql_query("UNLOCK TABLES;");
	}

	if($t == 1){
		echo "1";
	}elseif($t == -1){
		echo "-1";
	}elseif($t == 2){
		echo "2";
	}

}else
{
	echo "";
}

?>

